/* 
 * Copyright 2013 Imanuel Rohlfing, Dennis Wittkötter

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package management.entities;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * A user object which can be persisted to a database.
 * The user has an unique id, a name and password.
 * The password will converted to a md5 hash.
 * @author Imanuel Rohlfing
 */
@Entity
@ManagedBean(name="intuser")
@RequestScoped
public class IntUser implements Serializable{
    
    private static final long serialVersionUID = 1L;
    
    /**
     * a unique id of the user
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
        
    /**
     * the name of the user
     */
    private String username;
    
    /**
     * the password of the user converted to md5
     */
    private String password;

    /**
     * Default constructor
     */
    public IntUser(){}
    
    /**
     * Creates a user with the commited name and password.
     * The password will converted to a md5 hash.
     * @param username  the name of the user
     * @param password  the password of the user
     */
    public IntUser(String username, String password){
        this.username = username;
        try {
            this.password = md5(password);
        } catch (NoSuchAlgorithmException ex) {
            this.password = "";
        }
    }
    
    /**
     * Provides the id of the user.
     * @return  user id 
     */
    public int getId() {
        return id;
    }
    
    /**
     * Provides the name of the user.
     * @return  user name
     */
    public String getUsername() { 
        return username; 
    }

    /**
     * Sets the name of the user.
     * @param username  user name
     */
    public void setUsername(String username) { 
        this.username = username; 
    }

    /**
     * Provides the password.
     * @return  password
     */
    public String getPassword() { 
        return password; 
    }

    /**
     * Sets the password.
     * @param password  password
     */
    public void setPassword(String password) { 
        try {
            this.password = md5(password);
        } catch (NoSuchAlgorithmException ex) {
            this.password = "";
        }
    }
    
    /**
     * Converts the commited string to a md5 hash.
     * @param s hashable string
     * @return  converted string
     * @throws NoSuchAlgorithmException 
     */
    private String md5(String s) throws NoSuchAlgorithmException{
/* Berechnung */
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();
        md5.update(s.getBytes());
        byte[] result = md5.digest();

        /* Ausgabe */
        StringBuilder hexString = new StringBuilder();
        for (int i=0; i<result.length; i++) {
            if(result[i] <= 15 && result[i] >= 0){
		hexString.append("0");
            }
            hexString.append(Integer.toHexString(0xFF & result[i]));
        }
        return hexString.toString();
    }
}